﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sharp3D.Math.Core;

namespace GLWrap.SceneObjects
{
    public class FullfilledLine: SceneObjectsGroup
    {
        private const float DEFAULT_WEIGHT = 0.01f;
        private Vector3F _pointA;
        private Vector3F _pointB;
        private SimpleLine _line;
        private Sphere _pointASphere;
        private Sphere _pointBSphere;
        private float _weight;
        private bool _dirty;

        public Vector3F PointA
        {
            get { return _pointA; }
            set
            {
                _pointA = value;
                _dirty = true;
            }
        }

        public Vector3F PointB
        {
            get { return _pointB; }
            set
            {
                _pointB = value;
                _dirty = true;
            }
        }

        public SimpleLine Line
        {
            get { return _line; }
            set
            {
                _line = value;
                _dirty = true;
            }
        }

        public float Weight
        {
            get { return _weight; }
            set
            {
                _weight = value;
                _line.Weight = _weight;
                _pointASphere.Radius = _weight;
                _pointBSphere.Radius = _weight;
            }
        }

        public FullfilledLine()
        {
            _dirty = true;

            _line = new SimpleLine();
            _pointASphere = new Sphere();
            _pointBSphere = new Sphere();

            Weight = DEFAULT_WEIGHT;

            AttachObject(_line);
            AttachObject(_pointASphere);
            AttachObject(_pointBSphere);
        }

        protected override void GroupWillBeDrawn(GlSceneManager sceneManager)
        {
            RecalculatePositions();
        }

        private void RecalculatePositions()
        {
            if (!_dirty) return;

            _line.PointA = PointA;
            _line.PointB = PointB;
            _pointASphere.Position = PointA;
            _pointBSphere.Position = PointB;

            _dirty = false;
        }
    }
}
